package com.whmall.service.crm.module.company.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whmall.common.enumeration.YesNoEnum;
import com.whmall.component.entity.dto.OperatorReqDTO;
import com.whmall.component.entity.dto.PageReqDTO;
import com.whmall.component.entity.dto.PageRspDTO;
import com.whmall.service.crm.entity.dto.company.CompanyEventCreateReqDTO;
import com.whmall.service.crm.entity.dto.company.CompanyEventQueryReqDTO;
import com.whmall.service.crm.entity.dto.company.CompanyEventRspDTO;
import com.whmall.service.crm.entity.dto.company.CompanyEventUpdateReqDTO;
import com.whmall.service.crm.module.company.entity.CompanyEventDO;
import com.whmall.service.crm.module.company.service.CompanyEventService;
import com.whmall.service.crm.module.company.dao.CompanyEventMapper;
import lombok.extern.slf4j.Slf4j;
import com.whmall.component.service.AbstractDbService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;


@Slf4j
@Service
public class CompanyEventServiceImpl extends AbstractDbService<CompanyEventMapper, CompanyEventDO>
            implements CompanyEventService{

    @Override
    public PageRspDTO<CompanyEventRspDTO> page(PageReqDTO pageReqDTO, CompanyEventQueryReqDTO companyEventQueryReqDTO) {

        LambdaQueryWrapper<CompanyEventDO> queryWrapper = makeCondition(companyEventQueryReqDTO);

        IPage<CompanyEventDO> page = super.page(Page.of(pageReqDTO.getPageNum(),pageReqDTO.getPageSize()),queryWrapper);
        if(CollUtil.isEmpty(page.getRecords())){
            return PageRspDTO.EMPTY;
        }

        return PageRspDTO.of(pageReqDTO,(int) page.getPages(),(int) page.getTotal(),page.getRecords().stream().map(r -> {
            CompanyEventRspDTO rspDTO = new CompanyEventRspDTO();
            BeanUtils.copyProperties(r,rspDTO);
            return rspDTO;
        }).collect(Collectors.toList()));
    }

    @Override
    public void save(CompanyEventCreateReqDTO reqDTO, OperatorReqDTO operatorReqDTO) {
        CompanyEventDO companyEventDO = new CompanyEventDO();
        BeanUtils.copyProperties(reqDTO,companyEventDO);
        companyEventDO.setCreateId(operatorReqDTO.getOperatorId());
        companyEventDO.setCreateTime(new Date());
        super.save(companyEventDO);
    }

    @Override
    public void update(CompanyEventUpdateReqDTO reqDTO, OperatorReqDTO operatorReqDTO) {
        Assert.isTrue(null != reqDTO.getId() && null != super.getById(reqDTO.getId()),"请求参数异常");
        CompanyEventDO companyEventDO = new CompanyEventDO();
        BeanUtils.copyProperties(reqDTO,companyEventDO);
        companyEventDO.setUpdateId(operatorReqDTO.getOperatorId());
        companyEventDO.setUpdateTime(new Date());
        super.updateById(companyEventDO);
    }

    @Override
    public void removeById(Long id, OperatorReqDTO operatorReqDTO) {
        Assert.isTrue(null != id && null != super.getById(id),"请求参数异常");
        CompanyEventDO companyEventDO = new CompanyEventDO();
        companyEventDO.setId(id);
        companyEventDO.setIsDeleted(YesNoEnum.YES.getCode());
        companyEventDO.setUpdateId(operatorReqDTO.getOperatorId());
        companyEventDO.setUpdateTime(new Date());
        super.updateById(companyEventDO);
    }

    /**
     * 组装分页查询条件
     * @param queryReqDTO
     * @return
     */
    private LambdaQueryWrapper<CompanyEventDO> makeCondition(CompanyEventQueryReqDTO queryReqDTO){
        LambdaQueryWrapper<CompanyEventDO> queryWrapper = Wrappers.lambdaQuery();
        Map<SFunction<CompanyEventDO,?>,Object> map = new HashMap<>();
        map.put(CompanyEventDO::getCompanyId,queryReqDTO.getCompanyId());
        map.put(CompanyEventDO::getIsDeleted,queryReqDTO.getIsDeleted());
        map.put(CompanyEventDO::getSource,queryReqDTO.getSource());
        map.put(CompanyEventDO::getType1,queryReqDTO.getType1());
        queryWrapper.allEq(map,false);
        queryWrapper.ge(ObjectUtil.isNotNull(queryReqDTO.getTimeGte()),CompanyEventDO::getCreateTime,queryReqDTO.getTimeGte());
        queryWrapper.le(ObjectUtil.isNotNull(queryReqDTO.getTimeLte()),CompanyEventDO::getCreateTime,queryReqDTO.getTimeLte());
        queryWrapper.orderByDesc(CompanyEventDO::getCreateTime);
        return queryWrapper;
    }
}